package com.voute.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.voute.Model.Bean.SearchResultEntity;
import com.voute.R;
import com.voute.Utils.ViewHolderUtils;
import com.voute.View.Activity.SearchResultActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liucxu on 2017/1/14.
 */

public class RecommandWordsAdapter extends BaseAdapter {

    private List<SearchResultEntity> datas = new ArrayList<>();
    private Context context;

    public RecommandWordsAdapter(Context c, ArrayList<SearchResultEntity> entities) {
        if (entities != null) {
            datas.clear();
            datas.addAll(entities);
            context = c;
        }
    }

    public void SetDatas(ArrayList<SearchResultEntity> entities) {
        if (entities != null) {
            datas.clear();
            datas.addAll(entities);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convert_view, ViewGroup viewGroup) {
        if (convert_view == null) {
            convert_view = View.inflate(context, R.layout.adapter_hot_words, null);
        }
        initViews(i,convert_view);
        return convert_view;
    }

    private void initViews(int position,View convert_view){
        final SearchResultEntity entity=datas.get(position);
        TextView adapter_hot_words = ViewHolderUtils.get(convert_view,R.id.adapter_hot_words);
        adapter_hot_words.setText(entity.getTitle());
        convert_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,SearchResultActivity.class);
                intent.putExtra("query_words",entity.getTitle());
                context.startActivity(intent);
            }
        });
    }
}
